package com.github.prontera.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RequestLogging;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.TalentOrder;
import com.github.prontera.model.request.TalentOrderRequest;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.ClientService;
import com.github.prontera.service.TalentOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = ApiVersion.MANAGE_V1_PREFIX+"/talentOrder")
@Api(value = "达人订单")
public class TalentOrderController {

	@Autowired
	private TalentOrderService talentOrderService;
	
	@Autowired
	private ClientService clientService;
	
	

	/**
	 * 获取查找达人订单
	 */
	
	@ApiOperation(value = "获取查找达人订单",notes = "获取查找达人订单")
	@ResponseBody
	@RequestMapping(value = "get", method = RequestMethod.POST)
	public ObjectDataResponse<TalentOrder> get(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrderRequest talentOrderRequest) {
		TalentOrder findOrder=new TalentOrder();
		BeanUtils.copyProperties(talentOrderRequest, findOrder);
		List<TalentOrder> talentOrders=null;
		if(talentOrderRequest.isHasConsume()){
			findOrder.setClientId(clientId);
			talentOrders=talentOrderService.findClientOrder(findOrder);
		}else{
			findOrder.setAcceptClientId(clientId);
			talentOrders=talentOrderService.findAcceptClientOrder(findOrder);
		}
		if(talentOrders==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误");
		}
		return new ObjectDataResponse<TalentOrder>(talentOrders.get(0));
	}
	
	
	/**
	 * 后台主动结束订单
	 */
	
	@ApiOperation(value = "后台主动结束订单",notes = "后台主动结束订单")
	@ResponseBody
	@RequestMapping(value = "endOrder", method = RequestMethod.POST)
	public ObjectDataResponse<Boolean> endOrder(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrderRequest talentOrderRequest) {
		TalentOrder talentOrder=talentOrderService.get(talentOrderRequest.getId());
		clientService.confirmTalent(talentOrder);
		return new ObjectDataResponse<Boolean>(true);
	}
	
	/**
	 *达人订单申请退款
	 */
	
	@ApiOperation(value = "达人订单申请退款",notes = "达人订单申请退款")
	@ResponseBody
	@RequestMapping(value = "refund", method = RequestMethod.POST)
	public ObjectDataResponse<TalentOrder> refund(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrder talentOrder) {
		if(talentOrder.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误!");
		}
		TalentOrder findOrder=talentOrderService.get(talentOrder.getId());
		if(findOrder.getStatus().getCode()>=1||findOrder.getAcceptStatus().getCode()>=1){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"订单暂时无发退款!");
		}
		if(talentOrderService.refund(findOrder)){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "更新订单状态错误");
		}
		return new ObjectDataResponse<TalentOrder>(findOrder);
	}

	
	/**
	 * 
	 * 
	 * 查找达人订单
	 */
	
	@ApiOperation(value = "查找达人订单",notes = "查找达人订单")
	@ResponseBody
	@RequestMapping(value = "find", method = RequestMethod.POST)
	public ObjectDataResponse<List<TalentOrder>> find(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId, @RequestBody TalentOrder findOrder) {
		return new ObjectDataResponse<List<TalentOrder>>(talentOrderService.find(findOrder));
	}
	
}
